using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SMSGateway.Entities.Models;
using SMSGateway.Entities.Utils;
using SMSGateway.Common;

namespace SMSGateway.DAL.Base
{
	public class AuthorDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public AuthorDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the tblAuthor table.
		/// </summary>
		public virtual void Insert(AuthorInfo authorInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", authorInfo.UserName),
				new SqlParameter("@Active", authorInfo.Active)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Author_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the tblAuthor table.
		/// </summary>
		public virtual void Update(AuthorInfo authorInfo)
		{
            string command = @"UPDATE tblAuthor SET Active = @Active WHERE UserName = @UserName";
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", authorInfo.UserName),
				new SqlParameter("@Active", authorInfo.Active)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.Text, command, parameters);
		}

		/// <summary>
		/// Deletes a record from the tblAuthor table by its primary key.
		/// </summary>
		public virtual void Delete(string userName)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", userName)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Author_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the tblAuthor table.
		/// </summary>
		public virtual AuthorInfo Select(string userName)
		{
            string command = "SELECT UserName,Active FROM tblAuthor WHERE UserName = @UserName";
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", userName)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.Text, command, parameters))
			{
				if (dataReader.Read())
				{
					return MakeAuthorInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the tblAuthor table.
		/// </summary>
		public List<AuthorInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Author_SelectAll"))
			{
				List<AuthorInfo> authorInfoList = new List<AuthorInfo>();
				while (dataReader.Read())
				{
					authorInfoList.Add(MakeAuthorInfo(dataReader));
				}

				return authorInfoList;
			}
		}
        /// <summary>
        /// Selects with username records from the tblAuthor table.
        /// </summary>
        public List<AuthorInfo> SelectWithUserName(string userName)
        {
            string command = "SELECT UserName,Active FROM tblAuthor WHERE UserName = @UserName";
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", userName)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.Text, command, parameters))
            {
                List<AuthorInfo> authorInfoList = new List<AuthorInfo>();
                while (dataReader.Read())
                {
                    authorInfoList.Add(MakeAuthorInfo(dataReader));
                }

                return authorInfoList;
            }
        }
		/// <summary>
		/// Creates a new instance of the tblAuthor class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual AuthorInfo MakeAuthorInfo(SqlDataReader dataReader)
		{
			AuthorInfo authorInfo = new AuthorInfo();
			authorInfo.UserName = SqlClientUtility.GetString(dataReader, DbConstants.AUTHOR.USERNAME);
			authorInfo.Active = SqlClientUtility.GetBoolean(dataReader, DbConstants.AUTHOR.ACTIVE);

			return authorInfo;
		}

		#endregion
	}
}
